package com.yft.dao.base;

import javax.servlet.http.HttpServletRequest;

import jxl.Cell;
import jxl.CellType;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.yft.entity.base.Pager;
import com.yft.entity.base.PagerImpl;

public class BaseUtils {
	public static boolean checkNullBlank(String[] array){
		if(array==null || array.length==0){
			return false;
		}
		for(int i=0;i<array.length;i++){
			if(array[i] != null && !"".equals(array[i].trim()))
				continue;
			else
				return false;
		}
		return true;
	}
	
	public static boolean checkNullBlank(String arg){
		if(arg==null || "".equals(arg.trim())){
			return false;
		}
		return true;
	}
	
	public static boolean canStringToInteger(String arg) {
		try {
			Integer.valueOf(arg);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
	
	public static String stringFilter(String str) {
		if(str==null) return null;
		str = str.replaceAll(String.valueOf((char)12288)," ");
		str = str.trim();
		str = str.replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", "");
		return str;
	}
	
	public static Pager getExtPager(HttpServletRequest request) {
		Pager pager = new PagerImpl();
		String limit = request.getParameter("limit");
		int pageSize = 10;
		if (limit != null && limit.length() > 0)
			pageSize = NumberUtils.toInt(limit) > 0 ? NumberUtils.toInt(limit) : pageSize;
		String start = request.getParameter("start");
		int page = 1;
		if (start != null && start.length() > 0)
			page = NumberUtils.toInt(start) / pageSize + 1;
		pager.setPageSize(pageSize);
		pager.setPage(page);
		return pager;
	}
	
	/**
	 * 判断是否数据结束（如果整行为空则视为结束）
	 * @param row
	 * @return
	 */
	public static boolean isEnd(Cell[] row){
		int i=1;
		if(row.length==0) return true;
		for(Cell cl:row){
			if(cl==null||cl.getType().equals(CellType.EMPTY)||"".equals(StringUtils.trim(cl.getContents()))){
				i++;
			}
		}
		if(i==row.length) return true;
		else return false;
	}
}
